package yq.test.demo.Book.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yq.test.demo.Book.domain.Books;
import yq.test.demo.Book.service.BookService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     *
     * 查询所有图书
     * 分页插件
     * @param model
     * @return
     */
    @GetMapping("/getAllBook")
    public String findAll(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<Books> list= bookService.findAll();
        PageInfo<Books> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "Book/home";
    }
    @RequestMapping("/bookManege")
    public String All(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<Books> list= bookService.findAll();
        PageInfo<Books> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "Book/BookManege";
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public ModelAndView delete(@PathVariable int id){
        bookService.deleteById(id);
        ModelAndView mav = new ModelAndView("redirect:/bookManege");
        return mav;
    }
//    /**
//     *
//     * 模糊查询
//     * @return
//     */
//    @RequestMapping( "dim")
//    public String dim(Model model, HttpServletRequest request){
//        String book =request.getParameter("book");
//        List<Books> books = bookService.findByBooks(book);
//        model.addAttribute("books",books);
//        return "Book/BookManege";
//    }


    /**
     * 根据id
     * 更新
     *
     *
     *
     *
     */


    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model,@PathVariable int id){
        Books books = bookService.get(id);
        model.addAttribute("book",books);
        return "Book/update";
    }
    //更新
    @PostMapping("/update")
    public String updateUser(Model model, Books books, HttpServletRequest request){
        String id = request.getParameter("id");
        Books booksById= bookService.get(Integer.parseInt(id));
        bookService.update(books);
        System.out.println(books);
        return "redirect:/bookManege";
    }
    /**
     *
     *
     * 新增书籍
     */
    @RequestMapping("add")
    public ModelAndView Add(){
        return new ModelAndView("Book/AddBook");

    }
    @RequestMapping("addBook")
    public ModelAndView addBook(Books books){
    bookService.add(books);
        ModelAndView mav= new ModelAndView("redirect:/getAllBook");
        return mav;
    }



}
